package com.yanqu.road.server.cmd;

import com.yanqu.road.connect.AgentServerLinkControl;
import com.yanqu.road.AgentServerNettyService;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.pb.login.LoginProto;
import com.yanqu.road.player.PlayerControl;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;

import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Cmd(code = Protocol.A_LOGIN_REQUEST, desc = "客户端开始登录游戏")
public class LoginGameCmd implements Command {

	private static Logger logger = LogManager.getLogger(Class.class.getName());

	@Override
	public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

		LoginProto.LoginReqMsg reqMsg = LoginProto.LoginReqMsg.parseFrom(packet.toByteArray());
		long userId = reqMsg.getUserId();

		int checkLoginTokenOpen = ChannelConfig.CHECK_LOGIN_TOKEN_OPEN;
		if (checkLoginTokenOpen == 1) {
			Long userIdByToken = PlayerControl.getUserIdByToken(reqMsg.getLoginToken());
			if (userIdByToken == null) {
//				logger.error("client login err，userId:{} time:{}, token:{}", userId, System.currentTimeMillis(), reqMsg.getLoginToken());
				return;
			}

			if (userIdByToken != userId) {
				return;
			}
		}


//		if (!PlayerControl.canLogin(userId)) {
//			ctx.channel().close();
//			logger.info("login frequently ，userId:{} time:{}", userId, System.currentTimeMillis());
//			return;
//		}

		logger.info("client login，userId:{} time:{}", userId, System.currentTimeMillis());

		PlayerControl.addChannelCache(userId, ctx.channel());

		String ip = ctx.channel().remoteAddress().toString();
		ip = ctx.attr(AgentServerNettyService.KEY_FOR_PLAYER_IP).get();
		logger.info("userId : {} , ip: {}, appExt:{}, appId:{}", userId, ip, reqMsg.getAppExt(), reqMsg.getAppId());
		LoginProto.LoginReqMsg.Builder checkMsg = LoginProto.LoginReqMsg.newBuilder();
		checkMsg.setUserId(userId);
		checkMsg.setIp(ip);
		checkMsg.setClientVer(reqMsg.getClientVer());
		checkMsg.setLangType(reqMsg.getLangType());
		checkMsg.setPhonePlatform(reqMsg.getPhonePlatform());
		checkMsg.setDeviceId(reqMsg.getDeviceId());
		checkMsg.setOpenId(reqMsg.getOpenId());
		checkMsg.setAppId(reqMsg.getAppId());
		checkMsg.setChildGameId(reqMsg.getChildGameId());
		checkMsg.setAppExt(reqMsg.getAppExt());
		checkMsg.setLiveShowType(reqMsg.getLiveShowType());
		checkMsg.setLoginLinkType(reqMsg.getLoginLinkType());
		checkMsg.setLoginToken(reqMsg.getLoginToken());

		if(reqMsg.hasZfbChannel()){
			checkMsg.setZfbChannel(reqMsg.getZfbChannel());
		}

		String sign = ctx.attr(AgentServerNettyService.KEY_FOR_SEC_WEBSOCKET_PROTOCOL).get();
		checkMsg.setSign(sign == null ? "" : sign);
		YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.S_AGENT_CHECK_LOGIN, userId, checkMsg);
		AgentServerLinkControl.routeGame(message);
	}
}
